/**
 * Copyright (c) 2018 兴道科技 All rights reserved.
 *
 * https://xingdao.com
 *
 * 版权所有，侵权必究！
 */

package com.slf.modules.log.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.slf.common.constant.Constant;
import com.slf.common.page.PageData;
import com.slf.common.service.impl.BaseServiceImpl;
import com.slf.common.utils.ConvertUtils;
import com.slf.modules.log.dao.SysRestLogOperationDao;
import com.slf.modules.log.dto.SysRestLogOperationDTO;
import com.slf.modules.log.entity.SysRestLogOperationEntity;
import com.slf.modules.log.service.SysRestLogOperationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 操作日志
 *
 * @author C同学 zaiculture@sina.com
 * @since 1.0.0
 */
@Service
public class SysRestLogOperationServiceImpl extends BaseServiceImpl<SysRestLogOperationDao, SysRestLogOperationEntity> implements SysRestLogOperationService {

    @Override
    public PageData<SysRestLogOperationDTO> page(Map<String, Object> params) {
        IPage<SysRestLogOperationEntity> page = baseDao.selectPage(
            getPage(params, Constant.CREATE_DATE, false),
            getWrapper(params)
        );

        return getPageData(page, SysRestLogOperationDTO.class);
    }

    @Override
    public List<SysRestLogOperationDTO> list(Map<String, Object> params) {
        List<SysRestLogOperationEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, SysRestLogOperationDTO.class);
    }

    private QueryWrapper<SysRestLogOperationEntity> getWrapper(Map<String, Object> params){
        String status = (String) params.get("status");

        QueryWrapper<SysRestLogOperationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(status), "status", status);

        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysRestLogOperationEntity entity) {
        insert(entity);
    }

}